/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kivuli.visao;

import java.util.HashMap;
import javax.swing.ButtonGroup;
import kivuli.entidade.Arvore;
import kivuli.entidade.Casa;
import kivuli.entidade.Figura;
import kivuli.entidade.Muro;
import kivuli.entidade.Poste;
import kivuli.entidade.Predio;
import kivuli.entidade.Projeto;
import kivuli.rn.PersistenciaRN;
import kivuli.visao.dialog.DialogProjeto;
import kivuli.visao.item.ItemArvore;
import kivuli.visao.item.ItemCasa;
import kivuli.visao.item.ItemMuro;
import kivuli.visao.item.ItemPoste;
import kivuli.visao.item.ItemPredio;

/**
 *
 * @author bpmlab
 */
public class FramePrincipal extends javax.swing.JFrame {

    private final HashMap<String, Projeto> mapaDeProjetos = new HashMap<>();

    public FramePrincipal() {
        initComponents();
    }

    public ButtonGroup getGrupoDeFerramentas() {
        return grupoDeFerramentas;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupoDeFerramentas = new javax.swing.ButtonGroup();
        panelCabecalho = new javax.swing.JPanel();
        toolBar = new javax.swing.JToolBar();
        btnNovo = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnAbrir = new javax.swing.JButton();
        panelRodape = new javax.swing.JPanel();
        labelPosicaoMouse = new javax.swing.JLabel();
        panelPaleta = new javax.swing.JPanel();
        toolBarFiguras = new javax.swing.JToolBar();
        tgbCasa = new javax.swing.JToggleButton();
        tgbPredio = new javax.swing.JToggleButton();
        tgbPoste = new javax.swing.JToggleButton();
        tgbMuro = new javax.swing.JToggleButton();
        tgbArvore = new javax.swing.JToggleButton();
        abasProjeto = new javax.swing.JTabbedPane();
        menuBarra = new javax.swing.JMenuBar();
        menuArquivo = new javax.swing.JMenu();
        menuItemNovo = new javax.swing.JMenuItem();
        menuItemAbrir = new javax.swing.JMenuItem();
        menuItemSalvar = new javax.swing.JMenuItem();
        menuItemSalvarComo = new javax.swing.JMenuItem();
        menuItemFechar = new javax.swing.JMenuItem();
        menuItemPropriedades = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        menuItemSair = new javax.swing.JMenuItem();
        menuFerramentas = new javax.swing.JMenu();
        menuItemSimulacao = new javax.swing.JMenuItem();
        menuFiguras = new javax.swing.JMenu();
        checkCasa = new javax.swing.JCheckBoxMenuItem();
        checkPredio = new javax.swing.JCheckBoxMenuItem();
        checkPoste = new javax.swing.JCheckBoxMenuItem();
        checkMuro = new javax.swing.JCheckBoxMenuItem();
        checkArvore = new javax.swing.JCheckBoxMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setExtendedState(MAXIMIZED_BOTH);

        panelCabecalho.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelCabecalho.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEADING, 0, 0));

        toolBar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        toolBar.setRollover(true);

        btnNovo.setText("Novo");
        btnNovo.setFocusable(false);
        btnNovo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNovo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });
        toolBar.add(btnNovo);

        btnSalvar.setText("Salvar");
        btnSalvar.setFocusable(false);
        btnSalvar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSalvar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });
        toolBar.add(btnSalvar);

        btnAbrir.setText("Abrir");
        btnAbrir.setFocusable(false);
        btnAbrir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAbrir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAbrirActionPerformed(evt);
            }
        });
        toolBar.add(btnAbrir);

        panelCabecalho.add(toolBar);

        getContentPane().add(panelCabecalho, java.awt.BorderLayout.NORTH);

        panelRodape.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.TRAILING));
        panelRodape.add(labelPosicaoMouse);

        getContentPane().add(panelRodape, java.awt.BorderLayout.SOUTH);

        panelPaleta.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelPaleta.setLayout(new javax.swing.BoxLayout(panelPaleta, javax.swing.BoxLayout.Y_AXIS));

        toolBarFiguras.setOrientation(javax.swing.SwingConstants.VERTICAL);
        toolBarFiguras.setRollover(true);

        grupoDeFerramentas.add(tgbCasa);
        tgbCasa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/casa.png"))); // NOI18N
        tgbCasa.setActionCommand("CASA");
        tgbCasa.setFocusable(false);
        tgbCasa.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tgbCasa.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBarFiguras.add(tgbCasa);

        grupoDeFerramentas.add(tgbPredio);
        tgbPredio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/predio32x32.png"))); // NOI18N
        tgbPredio.setActionCommand("PREDIO");
        tgbPredio.setFocusable(false);
        tgbPredio.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tgbPredio.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBarFiguras.add(tgbPredio);

        grupoDeFerramentas.add(tgbPoste);
        tgbPoste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/poste32x32.png"))); // NOI18N
        tgbPoste.setActionCommand("POSTE");
        tgbPoste.setFocusable(false);
        tgbPoste.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tgbPoste.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBarFiguras.add(tgbPoste);

        grupoDeFerramentas.add(tgbMuro);
        tgbMuro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/muro32x32.png"))); // NOI18N
        tgbMuro.setActionCommand("MURO");
        tgbMuro.setFocusable(false);
        tgbMuro.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tgbMuro.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBarFiguras.add(tgbMuro);

        grupoDeFerramentas.add(tgbArvore);
        tgbArvore.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/arvore32x32.png"))); // NOI18N
        tgbArvore.setActionCommand("ARVORE");
        tgbArvore.setFocusable(false);
        tgbArvore.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tgbArvore.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        toolBarFiguras.add(tgbArvore);

        panelPaleta.add(toolBarFiguras);

        getContentPane().add(panelPaleta, java.awt.BorderLayout.WEST);
        getContentPane().add(abasProjeto, java.awt.BorderLayout.CENTER);

        menuArquivo.setText("Arquivo");

        menuItemNovo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        menuItemNovo.setText("Novo");
        menuItemNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemNovoActionPerformed(evt);
            }
        });
        menuArquivo.add(menuItemNovo);

        menuItemAbrir.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        menuItemAbrir.setText("Abrir");
        menuItemAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemAbrirActionPerformed(evt);
            }
        });
        menuArquivo.add(menuItemAbrir);

        menuItemSalvar.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        menuItemSalvar.setText("Salvar");
        menuItemSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemSalvarActionPerformed(evt);
            }
        });
        menuArquivo.add(menuItemSalvar);

        menuItemSalvarComo.setText("Salvar Como");
        menuArquivo.add(menuItemSalvarComo);

        menuItemFechar.setText("Fechar");
        menuArquivo.add(menuItemFechar);

        menuItemPropriedades.setText("Propriedades");
        menuArquivo.add(menuItemPropriedades);
        menuArquivo.add(jSeparator1);

        menuItemSair.setText("Sair");
        menuArquivo.add(menuItemSair);

        menuBarra.add(menuArquivo);

        menuFerramentas.setText("Ferramentas");

        menuItemSimulacao.setText("Simulação");
        menuFerramentas.add(menuItemSimulacao);

        menuFiguras.setText("Figuras");

        grupoDeFerramentas.add(checkCasa);
        checkCasa.setSelected(true);
        checkCasa.setText("Casa");
        checkCasa.setActionCommand("CASA");
        menuFiguras.add(checkCasa);

        grupoDeFerramentas.add(checkPredio);
        checkPredio.setText("Prédio");
        checkPredio.setActionCommand("PREDIO");
        menuFiguras.add(checkPredio);

        grupoDeFerramentas.add(checkPoste);
        checkPoste.setText("Poste");
        checkPoste.setActionCommand("POSTE");
        menuFiguras.add(checkPoste);

        grupoDeFerramentas.add(checkMuro);
        checkMuro.setText("Muro");
        checkMuro.setActionCommand("MURO");
        menuFiguras.add(checkMuro);

        grupoDeFerramentas.add(checkArvore);
        checkArvore.setText("Árvore");
        checkArvore.setActionCommand("ARVORE");
        menuFiguras.add(checkArvore);

        menuFerramentas.add(menuFiguras);

        menuBarra.add(menuFerramentas);

        setJMenuBar(menuBarra);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        DialogProjeto dialog = new DialogProjeto(this);
        dialog.setVisible(true);
        if (dialog.isOk()) {
            Projeto projeto = dialog.getProjeto();
            AreaDesenho area = new AreaDesenho(this);
            area.setMousePosicao(labelPosicaoMouse);
            area.setProjeto(projeto);
            this.mapaDeProjetos.put(projeto.getTitulo(), projeto);
            this.abasProjeto.addTab(projeto.getTitulo(), area);
        }
    }//GEN-LAST:event_btnNovoActionPerformed

    private void menuItemNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemNovoActionPerformed
        btnNovoActionPerformed(evt);
    }//GEN-LAST:event_menuItemNovoActionPerformed

    private void menuItemSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemSalvarActionPerformed
        int index = this.abasProjeto.getSelectedIndex();
        String title = this.abasProjeto.getTitleAt(index);
        Projeto projeto = mapaDeProjetos.get(title);
        PersistenciaRN.salvar(projeto);
    }//GEN-LAST:event_menuItemSalvarActionPerformed

    private void menuItemAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemAbrirActionPerformed
        Projeto projeto = PersistenciaRN.abrir();
        if (projeto != null) {
            AreaDesenho area = new AreaDesenho(this);
            area.setMousePosicao(labelPosicaoMouse);
            area.setProjeto(projeto);
            for (Figura figura : projeto.getFiguras()) {
                if (figura instanceof Arvore) {
                    area.add(new ItemArvore((Arvore) figura));
                } else if (figura instanceof Muro) {
                    area.add(new ItemMuro((Muro) figura));
                } else if (figura instanceof Casa) {
                    area.add(new ItemCasa((Casa) figura));
                } else if (figura instanceof Predio) {
                    area.add(new ItemPredio((Predio) figura));
                } else if (figura instanceof Poste) {
                    area.add(new ItemPoste((Poste) figura));
                }
            }
            abasProjeto.addTab(projeto.getTitulo(), area);
            area.repaint();
        }
    }//GEN-LAST:event_menuItemAbrirActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        menuItemSalvarActionPerformed(evt);
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAbrirActionPerformed
        menuItemAbrirActionPerformed(evt);
    }//GEN-LAST:event_btnAbrirActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException |
                InstantiationException |
                IllegalAccessException |
                javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new FramePrincipal().setVisible(true);
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane abasProjeto;
    private javax.swing.JButton btnAbrir;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JCheckBoxMenuItem checkArvore;
    private javax.swing.JCheckBoxMenuItem checkCasa;
    private javax.swing.JCheckBoxMenuItem checkMuro;
    private javax.swing.JCheckBoxMenuItem checkPoste;
    private javax.swing.JCheckBoxMenuItem checkPredio;
    private javax.swing.ButtonGroup grupoDeFerramentas;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JLabel labelPosicaoMouse;
    private javax.swing.JMenu menuArquivo;
    private javax.swing.JMenuBar menuBarra;
    private javax.swing.JMenu menuFerramentas;
    private javax.swing.JMenu menuFiguras;
    private javax.swing.JMenuItem menuItemAbrir;
    private javax.swing.JMenuItem menuItemFechar;
    private javax.swing.JMenuItem menuItemNovo;
    private javax.swing.JMenuItem menuItemPropriedades;
    private javax.swing.JMenuItem menuItemSair;
    private javax.swing.JMenuItem menuItemSalvar;
    private javax.swing.JMenuItem menuItemSalvarComo;
    private javax.swing.JMenuItem menuItemSimulacao;
    private javax.swing.JPanel panelCabecalho;
    private javax.swing.JPanel panelPaleta;
    private javax.swing.JPanel panelRodape;
    private javax.swing.JToggleButton tgbArvore;
    private javax.swing.JToggleButton tgbCasa;
    private javax.swing.JToggleButton tgbMuro;
    private javax.swing.JToggleButton tgbPoste;
    private javax.swing.JToggleButton tgbPredio;
    private javax.swing.JToolBar toolBar;
    private javax.swing.JToolBar toolBarFiguras;
    // End of variables declaration//GEN-END:variables
}
